package com.androidx.p013lv.base.utils;

import android.content.SharedPreferences;
import android.text.TextUtils;
import androidx.collection.SimpleArrayMap;
import com.androidx.p013lv.base.BaseSdk;
import com.androidx.p013lv.base.bean.ClassifyBean;
import com.androidx.p013lv.base.bean.GameAccountBean;
import com.androidx.p013lv.base.bean.UserAccount;
import com.androidx.p013lv.base.bean.UserInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/* renamed from: com.androidx.lv.base.utils.SpUtils */
public class SpUtils {
    private static SimpleArrayMap<String, SpUtils> SP_UTILS_MAP = new SimpleArrayMap<>();
    private String ACCOUNT = "account";
    private String CLASSIFY = "classify";
    String HISTORY = "History";
    String NOVEL_HISTORY = "NovelHistory";
    private String USER_GAME_ACCOUNT = "userGameAccount";
    private String USER_INFO = "userInfo";

    /* renamed from: sp */
    private SharedPreferences f180sp;

    public void setNovelHistory(String str) {
        if (!TextUtils.isEmpty(str)) {
            List list = (List) getBean(this.NOVEL_HISTORY, new TypeToken<List<String>>() {
                /* class com.androidx.p013lv.base.utils.SpUtils.C10591 */
            }.getType());
            if (list == null || list.size() == 0) {
                ArrayList arrayList = new ArrayList();
                arrayList.add(str);
                put(this.NOVEL_HISTORY, new Gson().toJson(arrayList));
                return;
            }
            Iterator it = list.iterator();
            while (true) {
                if (it.hasNext()) {
                    if (str.equals((String) it.next())) {
                        list.remove(str);
                        break;
                    }
                } else {
                    break;
                }
            }
            list.add(0, str);
            put(this.NOVEL_HISTORY, new Gson().toJson(list));
        }
    }

    public void clearNovelHistory() {
        put(this.NOVEL_HISTORY, "");
    }

    public List<String> getNovelHistory() {
        return (List) getBean(this.NOVEL_HISTORY, new TypeToken<List<String>>() {
            /* class com.androidx.p013lv.base.utils.SpUtils.C10602 */
        }.getType());
    }

    public void setHistory(String str) {
        if (!TextUtils.isEmpty(str)) {
            List list = (List) getBean(this.HISTORY, new TypeToken<List<String>>() {
                /* class com.androidx.p013lv.base.utils.SpUtils.C10613 */
            }.getType());
            if (list == null || list.size() == 0) {
                ArrayList arrayList = new ArrayList();
                arrayList.add(str);
                put(this.HISTORY, new Gson().toJson(arrayList));
                return;
            }
            Iterator it = list.iterator();
            while (true) {
                if (it.hasNext()) {
                    if (str.equals((String) it.next())) {
                        list.remove(str);
                        break;
                    }
                } else {
                    break;
                }
            }
            list.add(0, str);
            put(this.HISTORY, new Gson().toJson(list));
        }
    }

    public void clearHistory() {
        put(this.HISTORY, "");
    }

    public List<String> getHistory() {
        return (List) getBean(this.HISTORY, new TypeToken<List<String>>() {
            /* class com.androidx.p013lv.base.utils.SpUtils.C10624 */
        }.getType());
    }

    public void setClassify(List<ClassifyBean> list) {
        if (list == null || list.size() == 0) {
            put(this.CLASSIFY, "");
        } else {
            put(this.CLASSIFY, new Gson().toJson(list));
        }
    }

    public List<ClassifyBean> getClassify() {
        return (List) getBean(this.CLASSIFY, new TypeToken<List<ClassifyBean>>() {
            /* class com.androidx.p013lv.base.utils.SpUtils.C10635 */
        }.getType());
    }

    public GameAccountBean getUserGameAccount() {
        GameAccountBean gameAccountBean = (GameAccountBean) getBean(this.USER_GAME_ACCOUNT, GameAccountBean.class);
        return gameAccountBean == null ? new GameAccountBean() : gameAccountBean;
    }

    public void setUserGameAccount(GameAccountBean gameAccountBean) {
        if (gameAccountBean == null) {
            put(this.USER_GAME_ACCOUNT, "");
        } else {
            put(this.USER_GAME_ACCOUNT, new Gson().toJson(gameAccountBean));
        }
    }

    public UserInfo getUserInfo() {
        UserInfo userInfo = (UserInfo) getBean(this.USER_INFO, UserInfo.class);
        return userInfo == null ? new UserInfo() : userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        if (userInfo == null) {
            put(this.USER_INFO, "");
        } else {
            put(this.USER_INFO, new Gson().toJson(userInfo));
        }
    }

    public UserAccount getUserAccount() {
        UserAccount userAccount = (UserAccount) getBean(this.ACCOUNT, UserAccount.class);
        return userAccount == null ? new UserAccount() : userAccount;
    }

    public void setUserAccount(UserAccount userAccount) {
        if (userAccount == null) {
            put(this.ACCOUNT, "");
        } else {
            put(this.ACCOUNT, new Gson().toJson(userAccount));
        }
    }

    public static SpUtils getInstance() {
        return getInstance("");
    }

    public static SpUtils getInstance(String str) {
        if (isSpace(str)) {
            str = "spUtils";
        }
        SpUtils spUtils = SP_UTILS_MAP.get(str);
        if (spUtils != null) {
            return spUtils;
        }
        SpUtils spUtils2 = new SpUtils(str);
        SP_UTILS_MAP.put(str, spUtils2);
        return spUtils2;
    }

    private SpUtils(String str) {
        this.f180sp = BaseSdk.getContext().getSharedPreferences(str, 0);
    }

    public void put(String str, String str2) {
        put(str, str2, false);
    }

    public void put(String str, String str2, boolean z) {
        if (z) {
            this.f180sp.edit().putString(str, str2).commit();
        } else {
            this.f180sp.edit().putString(str, str2).apply();
        }
    }

    public String getString(String str) {
        return getString(str, "");
    }

    public <T> T getBean(String str, Class<T> cls) {
        String string = getInstance().getString(str);
        if (TextUtils.isEmpty(string)) {
            return null;
        }
        return (T) new Gson().fromJson(string, (Class) cls);
    }

    public <T> T getBean(String str, Type type) {
        String string = getInstance().getString(str);
        if (TextUtils.isEmpty(string)) {
            return null;
        }
        return (T) new Gson().fromJson(string, type);
    }

    public String getString(String str, String str2) {
        return this.f180sp.getString(str, str2);
    }

    public void put(String str, int i) {
        put(str, i, false);
    }

    public void put(String str, int i, boolean z) {
        if (z) {
            this.f180sp.edit().putInt(str, i).commit();
        } else {
            this.f180sp.edit().putInt(str, i).apply();
        }
    }

    public int getInt(String str) {
        return getInt(str, -1);
    }

    public int getInt(String str, int i) {
        return this.f180sp.getInt(str, i);
    }

    public void put(String str, long j) {
        put(str, j, false);
    }

    public void put(String str, long j, boolean z) {
        if (z) {
            this.f180sp.edit().putLong(str, j).commit();
        } else {
            this.f180sp.edit().putLong(str, j).apply();
        }
    }

    public long getLong(String str) {
        return getLong(str, -1);
    }

    public long getLong(String str, long j) {
        return this.f180sp.getLong(str, j);
    }

    public void put(String str, float f) {
        put(str, f, false);
    }

    public void put(String str, float f, boolean z) {
        if (z) {
            this.f180sp.edit().putFloat(str, f).commit();
        } else {
            this.f180sp.edit().putFloat(str, f).apply();
        }
    }

    public float getFloat(String str) {
        return getFloat(str, -1.0f);
    }

    public float getFloat(String str, float f) {
        return this.f180sp.getFloat(str, f);
    }

    public void put(String str, boolean z) {
        put(str, z, false);
    }

    public void put(String str, boolean z, boolean z2) {
        if (z2) {
            this.f180sp.edit().putBoolean(str, z).commit();
        } else {
            this.f180sp.edit().putBoolean(str, z).apply();
        }
    }

    public boolean getBoolean(String str) {
        return getBoolean(str, false);
    }

    public boolean getBoolean(String str, boolean z) {
        return this.f180sp.getBoolean(str, z);
    }

    public void put(String str, Set<String> set) {
        put(str, set, false);
    }

    public void put(String str, Set<String> set, boolean z) {
        if (z) {
            this.f180sp.edit().putStringSet(str, set).commit();
        } else {
            this.f180sp.edit().putStringSet(str, set).apply();
        }
    }

    public Set<String> getStringSet(String str) {
        return getStringSet(str, Collections.emptySet());
    }

    public Set<String> getStringSet(String str, Set<String> set) {
        return this.f180sp.getStringSet(str, set);
    }

    public Map<String, ?> getAll() {
        return this.f180sp.getAll();
    }

    public boolean contains(String str) {
        return this.f180sp.contains(str);
    }

    public void remove(String str) {
        remove(str, false);
    }

    public void remove(String str, boolean z) {
        if (z) {
            this.f180sp.edit().remove(str).commit();
        } else {
            this.f180sp.edit().remove(str).apply();
        }
    }

    public void clear() {
        clear(false);
    }

    public void clear(boolean z) {
        if (z) {
            this.f180sp.edit().clear().commit();
        } else {
            this.f180sp.edit().clear().apply();
        }
    }

    private static boolean isSpace(String str) {
        if (str == null) {
            return true;
        }
        int length = str.length();
        for (int i = 0; i < length; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}
